<?php


namespace App\Models\Admin;



use App\Models\BaseModel;

class Menu extends BaseModel {
    protected $table = "navigation";
    protected $fillable = [
        'parent_id',
        'icon',
        'uri',
        'is_link',
        'permission_id',
        'name',
        'type',
        'guard_name',
        'sequence',
        'site_id',
        'data'
    ];

    //侧边菜单列表带权限控制
    public function getAll() {
        $result = self::withoutGlobalScope('site_id_scope')->with("permission")->where(function($query){
            $query->where("site_id",0)->orWhere("site_id",getCurrentSiteId());
        })->where('guard_name',getCurrentUser()['guard_name'])->orderBy("sort","asc")->get();
        return $result;
    }

    //管理菜单，菜单列表
    public function getList($params) {
        $result = self::whereBuilder($params)->orderBy("sort","asc")->get();
        return $result;
    }

    //添加菜单
    public function store($params) {
        $params["uri"]=array_key_exists("uri_link",$params)&&$params["uri_link"]?$params['uri_link']:$params['uri'];
        $params["site_id"]=getCurrentSiteId();
        unset($params["uri_link"]);
        //父栏目取消uri
        self::where('id', $params["parent_id"])->update(["uri"=>""]);
        //创建
        return self::create($params);
    }

    //获取单个菜单
    public function getOne($id) {
        return self::find($id)->toArray();
    }

    //修改菜单
    public function updateMenu($params, $id) {
        unset($params["_token"]);
        if(array_key_exists("uri_link",$params)&&$params["uri_link"]!=null&&!empty($params["uri_link"])&&$params["uri_link"]!=""){
            $params["uri"]=$params['uri_link'];
        }
        unset($params["uri_link"]);
        unset($params["select"]);
        //父栏目取消uri
        self::where('id', $params["parent_id"])->update(["uri"=>""]);
        return self::where('id', $id)->update($params);
    }

    //删除菜单
    public function destoryOne($id) {
        return self::where("id", $id)->delete();
    }

    //面包屑,导航位置
    public function getPosition($routeName) {
        $routeName = preg_replace ("/.attach(.[\d]+){0,1}$/", "",$routeName);
        $arr = explode('.', $routeName);
        unset($arr[count($arr) - 1]);
        $serarr = implode(".", $arr);
        $nav = "<a href='/admin'>首页</a>";
        $now = self::withoutGlobalScope('site_id_scope')->where('uri', 'like', '%' . $serarr . '%')->get(["parent_id", "name", "uri","data"])->toArray();
        $url = empty($now[0]["data"])?route($now[0]['uri']):route($now[0]['uri'],json_decode($now[0]["data"],true));
        if (!empty($now)) {
            $nav .= "<span lay-separator=''>/</span><a href=" . $url . " ><cite>" . $now[0]["name"] . "</cite></a>";
        }
        return $nav;
    }

    //获取父栏目信息
    public function getFInfo($parent_id){
        return self::find($parent_id);
    }

    //更新权限
    public function up_permission($menu_id,$permission_id){
        return self::where("id",$menu_id)->update(["permission_id"=>$permission_id]);
    }

    //关联权限
    public function permission(){
        return $this->hasOne("App\Models\Admin\Permission","id","permission_id");
    }
}
